package org.pathlet.mybatis;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MybatisFactory {

    private DataSource dataSource;

    //类型别名包扫描路径，多个用逗号分隔
    private String typeAliasesPackage;

    //mapper文件路径可以通过ant通配规则匹配，多个用逗号分隔
    private String mapperLocations;

    //mybatis配置文件路径
    private String configLocation;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setTypeAliasesPackage(String typeAliasesPackage) {
        this.typeAliasesPackage = typeAliasesPackage;
    }

    public void setMapperLocations(String mapperLocations) {
        this.mapperLocations = mapperLocations;
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
        StringUtils.split(configLocation, ",");
    }

    public SqlSessionFactory getSessionFactory() {
        try {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setTypeAliasesPackage(typeAliasesPackage);
            factoryBean.setMapperLocations(resolveMapperLocations(split(mapperLocations)));
            factoryBean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));

            return factoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private Resource[] resolveMapperLocations(String[] mapperLocations) {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<Resource> resources = new ArrayList<Resource>();
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                }
                catch (IOException e) {
                    // ignore
                }
            }
        }
        return resources.toArray(new Resource[resources.size()]);
    }

    /**
     * 逗号分隔字符串转换为数组
     * @param source
     * @return
     */
    private static String[] split(String source) {
        if (source == null) {
            return  new String[0];
        }
        else {
            String trimmed = source.trim();
            if (trimmed.length() == 0) {
                return new String[0];
            }

            return trimmed.split(",");
        }
    }


}
